package com.sunxueqi.project.blog.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sunxueqi.framework.web.controller.BaseController;
import com.sunxueqi.framework.web.vo.ResponseData;
import com.sunxueqi.framework.web.vo.ResponseTableData;
import com.sunxueqi.project.blog.service.IBlogService;
import com.sunxueqi.project.blog.vo.Blog;

import cn.hutool.core.util.StrUtil;

@RestController
@RequestMapping("/blog")
public class BlogController extends BaseController{

	@Autowired
	private IBlogService blogService;
	
	/**
	 * 查询博客列表
	 * @param blog
	 * @return
	 */
	@GetMapping("/list")
	public ResponseTableData list(Blog blog) {
		startPage();
		List<Blog> list = blogService.selectBlogList(blog);
		return getResponseTableData(list);
	}
	
	/**
	 * 查询某一条博客的详情信息
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public ResponseData getInfo(@PathVariable String id) {
		return ResponseData.success(blogService.selectBlogById(id));
	}
	
	/**
	 * 新增博客
	 * @param blog
	 * @return
	 */
	@PostMapping("/add")
	public ResponseData add(@RequestBody Blog blog) {
		return super.getResponseData(blogService.insertBlog(blog));
	}
	
	/**
	 * 修改博客
	 * @param blog
	 * @return
	 */
	@PutMapping("/update")
	public ResponseData update(@RequestBody Blog blog) {
		return super.getResponseData(blogService.updateBlog(blog));
	}
	
	/**
	 * 删除博客
	 */
	@PostMapping("/delete")
	public ResponseData remove(String ids) {
		String[] idArray = StrUtil.split(ids, ",");
		return super.getResponseData(blogService.deleteBlogById(idArray));
	}

	
	
}
